package game;

import java.io.File;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;

public class AudioEngine {

	private static final float MAX_PITCH_SHIFT = 5f;
	private static final float MAX_VOLUME_SHIFT = 0.25f;
	
	private static float maxVolume = 1f;
	
	private static Music bg, win;
	private static Sound charge, select, shoot, die;
	
	public static void loadAudio(){
		try{
			bg = new Music("resources" + File.separator + "audio" + File.separator + "bg.ogg");
			win = new Music("resources" + File.separator + "audio" + File.separator + "bg_end.ogg");
			
			charge = new Sound("resources" + File.separator + "audio" + File.separator + "charge.wav");
			select = new Sound("resources" + File.separator + "audio" + File.separator + "select.wav");
			shoot = new Sound("resources" + File.separator + "audio" + File.separator + "shoot.wav");
			die = new Sound("resources" + File.separator + "audio" + File.separator + "die.wav");
			
		} catch(SlickException e){
			System.err.println("Error loading audio");
			e.printStackTrace();
		}
	}
	
	public static void playCharge(float sightedness){
		float pitchShift = sightedness * MAX_PITCH_SHIFT;
		float volumeShift = sightedness * MAX_VOLUME_SHIFT;
		charge.play(1f+pitchShift, (0.25f+volumeShift) * maxVolume);
	}
	
	public static void playShoot(){
		shoot.play(1f, maxVolume/2);
	}
	
	public static void startBackgroundMusic(){
		bg.loop(1f, maxVolume);
	}
	
	public static void startWinMusic(){
		float pos = bg.getPosition();
		bg.stop();
		win.setPosition(pos);
		win.loop(1f, maxVolume);
	}
	
	/**
	 * Sets maximum volume
	 * 
	 * @param maxVol (between 0f and 1f)
	 */
	public static void setMaxVolume(float maxVol){
		maxVolume = maxVol;
		bg.setVolume(maxVol);
	}

	public static void stopBackgroundMusic() {
		bg.stop();
	}
}
